package com.depot.sys.web;

import java.util.HashMap;

import com.base.LoginUser;
import com.depot.DepotConstants;
import com.depot.domains.Users;
import com.depot.sys.dao.UserLoginDao;
import com.opensymphony.xwork2.ModelDriven;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;
import com.soar.senv.SoarProperties;

/**
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 * @description com.itaskstar.sys.web.UserLoginAction
 */
public class UserLoginAction extends BaseActionSupport implements ModelDriven<Users>
{
	private static final long serialVersionUID = 1L;

	public Users domain = new Users();

	public HashMap<String, String> users = new HashMap<String, String>();

	public void execute(Environment env) throws Exception
	{
		this.doLogin(env);
 	}
	
	public String doLogin(Environment env) throws Exception
	{
		String logindec = "";
		String loginStatus = "0";
		if (domain.getMobile() == null || "".equals(domain.getMobile()))
		{
			env.getResponse().sendRedirect(SoarProperties.SOAR_WEB_CONTEXTROOT + "/login.jsp");
			return null;
		}
		String vcode = (String) env.getRequest().getSession().getAttribute("validateCode");

		String validateCode = env.getRequest().getParameter("validateCode");
		if (!vcode.equals(validateCode))
		{
			logindec = "验证码不正确";
			env.getResponse().getWriter().print("{\"result\": \"2\",\"errmsg\": \"请输入正确的验证码\"}");
			return null;
		}

		HashMap<String, String> usermap = UserLoginDao.dologin(env, domain);
		if (null != usermap)
		{
			String rolecode = UserLoginDao.getRolecodeByUserid(env, usermap.get("ID"));
			if (null == rolecode)
			{
				logindec = "用户没权限";
				env.getResponse().getWriter().print("{\"result\": \"2\",\"errmsg\": \"没权限\"}");
				return null;
			}
			if (!DepotConstants.ADMIN_CODE.equals(rolecode))
			{
				if ("2".equals(UserLoginDao.userOrgStatus(env, domain)))
				{
					logindec = "用户组织机构不存在";
					env.getResponse().getWriter().print("{\"result\": \"2\",\"errmsg\": \"所属组织机构不存在\"}");
					return null;
				}
				else if ("0".equals(UserLoginDao.userOrgStatus(env, domain)))
				{
					logindec = "用户组织机构被禁";
					env.getResponse().getWriter().print("{\"result\": \"2\",\"errmsg\": \"所属组织机构被禁\"}");
					return null;
				}
			}
			LoginUser user = new LoginUser();
			user.setUserId(usermap.get("ID"));
			user.setUserName(usermap.get("NAME"));
			user.setUserOrgId(usermap.get("ORG_ID"));
			user.setUserOrgCode(usermap.get("ORG_CODE"));
			user.setUserDeptId(usermap.get("DEP_ID"));
			user.setUserOrgName(usermap.get("ORG_NAME"));

			user.setUserRoleCode(rolecode);
			env.getRequest().getSession().setAttribute(SoarProperties.SOAR_BM_LOGINUSER_SESSION_ID, user);
			env.getLoginUser().loadAuth(env);

			logindec = "登录成功";
			loginStatus = "1";
			env.getResponse().getWriter().print("{\"result\": \"1\",\"errmsg\": \"\"}");
		}
		else
		{
			logindec = "用户名密码不正确";
			env.getResponse().getWriter().print("{\"result\": \"2\",\"errmsg\": \"请输入正确的用户名和密码\"}");
		}
		UserLoginDao.loginRecord(env, logindec, domain.getMobile(), loginStatus);
		return null;
	}

	public String doWelcome(Environment env) throws Exception
	{
		return "index";
	}

	public Users getModel()
	{
		return this.domain;
	}
}
